<template>
  <div>
    <!-- 提交地址表单 -->
    <div class="address-form">
      <el-form
        label-width="100px"
        ref="addressFormRef"
        :model="addressForm"
        :rules="addressFormRules"
      >
        <el-form-item label="收件人" prop="name">
          <el-input v-model="addressForm.name"></el-input>
        </el-form-item>

        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="addressForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="content">
          <el-input type="textarea" v-model="addressForm.content"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('addressFormRef')">新增地址</el-button>
          <el-button @click="resetForm('addressFormRef')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 地址列表 -->
    <div class="address-table">
      <el-table :data="addressTable" border style="width: 100%">
        <!-- <el-table-column prop="id" label="编号" width="50"></el-table-column> -->
        <el-table-column prop="name" label="姓名" width="120"></el-table-column>
        <el-table-column prop="phone" label="手机号" width="120"></el-table-column>
        <el-table-column prop="content" label="地址" width="500"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="showEditAddressForm(scope.row.id, scope.row)"
            >编辑</el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.row.id,scope.$index)"
              type="text"
              size="small"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 修改地址对话框 -->
    <div>
      <el-dialog title="提示" :visible.sync="editAddressVisible" width="30%">
        <!-- 修改地址表单 -->
        <el-form
          label-width="100px"
          ref="editAddressFormRef"
          :model="editAddressForm"
          :rules="editAddressFormRules"
        >
          <el-form-item label="id" prop="id">
            <el-input v-model="editAddressForm.id" disabled></el-input>
          </el-form-item>

          <el-form-item label="收件人" prop="name">
            <el-input v-model="editAddressForm.name"></el-input>
          </el-form-item>

          <el-form-item label="手机号码" prop="phone">
            <el-input v-model="editAddressForm.phone"></el-input>
          </el-form-item>
          <el-form-item label="详细地址" prop="content">
            <el-input type="textarea" v-model="editAddressForm.address"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editAddressVisible = false">取 消</el-button>
          <el-button type="primary" @click="editAddressVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 检测手机号
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/;
        console.log(reg.test(value));
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    return {
      //新增地址表单
      addressForm: {
        name: "",
        phone: "",
        content: ""
      },

      //编辑地址表单
      editAddressForm: {},

      //显示对话框布尔值
      editAddressVisible: false,

      // 地址列表
      addressTable: [
        {
          id: 0,
          name: "大咸鱼子",
          phone: "8008820",
          content: "辽宁省大连市东北那嘎嘎"
        }
      ],

      //新增地址表单验证
      addressFormRules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { validator: checkPhone, trigger: "blur" }
        ],
        address: [
          { required: true, message: "请输入地址", trigger: "blur" },
          { min: 6, message: "长度大于 6 个字符 ", trigger: "blur" }
        ]
      },

      //修改地址表单验证规则
      editAddressFormRules: {
        id: [{ required: true, message: "请输入id", trigger: "blur" }],
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 1, max: 10, message: "长度在 1 到 10 个字符", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { validator: checkPhone, trigger: "blur" }
        ],
        address: [
          { required: true, message: "请输入地址", trigger: "blur" },
          { min: 6, message: "长度大于 6 个字符 ", trigger: "blur" }
        ]
      }
    };
  },
  mounted() {
    const user = window.sessionStorage.getItem("user");

    // if(user){

    //     this.$http.post('/user/getMyAddress').then(res=>{

    //       const response = res.data;
    //       console.log(response)
    //       if(response.code == 200){

    //         this.addressTable = response.data.userAddress;
    //         console.log(this.addressTable)
    //       }

    //     }).catch(function (error) {

    //         console.log(error);

    //     })
    // }

    this.getAddressList();
  },
  methods: {
    // 新增收货地址
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {

        //添加成功
        if (valid) {
          const { data: res } = await this.$http.post(
            "user/addAddress",
            this.addressForm
          );
          if (res.code == 200) {

            this.addressTable.push(this.addressForm);

            this.$message({
              message: "恭喜你，这是一条成功消息",
              type: "success"
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.$message("已重置");
    },

    // 展示编辑地址框
    showEditAddressForm(id, row) {
      var that = this;
      that.editAddressVisible = true;
      that.editAddressForm = row;
    },
    // 删除地址
    deleteRow(id, index) {
      var that = this;

      this.$confirm("确定删除该地址 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          // that.rows.splice(id, 1);
          // console.log(index);
          // console.log(id);

          const { data: res } = await this.$http.post("user/deleteAddress", {
            addressId: id
          });
          if (res.code == 200) {
            that.addressTable.splice(index, 1);
            // 删除成功提示
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        })
        .catch(() => {
          // 错误提示
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 获得该用户地址列表
    async getAddressList() {
      const { data: res } = await this.$http.get("user/getMyAddress/1");

      if (res.code == 200) {
        this.addressTable = res.data.userAddress;
      }
      // console.log(res);
    }
  }
};
</script>

<style lang="less" scoped>
.address-form {
  width: 20%;
  margin: auto;
  position: relative;
  top: 100px;
}
.address-table {
  width: 45%;
  margin: auto;
  position: relative;
  top: 150px;
}
</style>
